package com.fly.control;

import java.sql.ResultSet;

import com.fly.conn.DBConnect;
import com.fly.model.DailyCost;

public class DailyCostControl {
	public static boolean insertDailyCost(DailyCost d){
		DBConnect dbc = new DBConnect();
		String sql = "insert into dailycost(preson ,detail ,time) values(? ,? ,?)";
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setString(i++, d.getPerson());
			dbc.setString(i++, d.getDetail());
			dbc.setString(i++, d.getTime());
			dbc.executeUpdate();
			return true;
		} catch (Exception e){
			e.printStackTrace();
			System.out.println("insertDailyCost ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean deleteDailyCost(int id){
		String sql = "delete * from dailycost where id =" + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("deleteDailyCost ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean updateDailyCost(int id, DailyCost d){
		String sql = "update dailycost set person = ?, detail = ?,time = ? where id = " + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("updateDailyCost ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static DailyCost getDailyCost(int id){
		String sql = "select * from dailycost where id = " + id;
		ResultSet rs = null;
		DailyCost dc = new DailyCost();
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				dc.setPerson(rs.getString("person"));
				dc.setDetail(rs.getString("detail"));
				dc.setTime(rs.getString("time"));
			}
			return dc;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("getDailyCost ERROR");
		} finally{
			dbc.close();
		}
		return null;
	}
}
